package com.maotouying.owlsmart.homePage;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.maotouying.owlsmart.R;

import com.maotouying.owlsmart.SmartLayout.PageMenu.IndicatorView;
import com.maotouying.owlsmart.SmartLayout.PageMenu.PageMenuLayout;
import com.maotouying.owlsmart.SmartLayout.PageMenu.ScreenUtil;
import com.maotouying.owlsmart.SmartLayout.PageMenu.holder.AbstractHolder;
import com.maotouying.owlsmart.SmartLayout.PageMenu.holder.PageMenuViewHolderCreator;
import com.maotouying.owlsmart.SmartUtil.NoScrollGridView;

import com.maotouying.owlsmart.SmartUtil.SystemUtil;
import com.maotouying.owlsmart.magnetPage.magnetActivity;
import com.maotouying.owlsmart.moviePage.List.list_A;
import com.maotouying.owlsmart.moviePage.List.list_C;
import com.maotouying.owlsmart.moviePage.movieListActivity;
import com.maotouying.owlsmart.tvPage.tvListActivity;
import com.maotouying.owlsmart.youngLadyPage.youngladyDetailActivity;
import com.maotouying.owlsmart.youngLadyPage.youngladyListActivity;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class homeActivity extends AppCompatActivity {


    List<ModelHomeEntrance> homeEntrances;
    IndicatorView entranceIndicatorView;
    PageMenuLayout<ModelHomeEntrance> mPageMenuLayout;
    private List<younglady_C> younglady_cs= new ArrayList<younglady_C>();
    private ImageView iv_search;
    private EditText et_search;

    private younglady_C younglady_c;
    private younglady_A younglady_a;

    private NoScrollGridView lv_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        //顶部框颜色
        SystemUtil.setStatusBarColor(this, this.getResources().getColor(R.color.top_background_color));

        //视图
        lv_list =(NoScrollGridView)findViewById(R.id.lv_list);

        //屏幕适配绑定，一定要写，否则加载不出来
        ScreenUtil.init(homeActivity.this);
        //加载可滑动的导航栏
        initView();
        initData();
        init();

        //初始化数据
        okhttp_text("爱爱");
    }




    private void initView() {
        entranceIndicatorView = (IndicatorView)findViewById(R.id.main_home_entrance_indicator);
        mPageMenuLayout = (PageMenuLayout)findViewById(R.id.pagemenu);


        et_search=  (EditText) findViewById(R.id.et_search);
        et_search.setOnEditorActionListener(new mEditorActionListener());
        //搜索按钮点击事件
        iv_search =  (ImageView) findViewById(R.id.iv_search);
        iv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = "";
                if(et_search.getText().toString().isEmpty()){
                    content = homeActivity.this.getString(R.string.app_net);
                }else {
                    content=et_search.getText().toString();
                }
                Toast.makeText(homeActivity.this, content, Toast.LENGTH_SHORT).show();
            }
        });
    }


    private class mEditorActionListener implements TextView.OnEditorActionListener {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            switch (v.getId()) {
                case R.id.et_search://搜索
                    if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                        et_search.setText("");
                    }
                    break;
            }
            return false;
        }
    }


    //初始化数据
    private void initData() {
        homeEntrances = new ArrayList<>();
        //第1页显示
        //  homeEntrances.add(new ModelHomeEntrance("公告", R.drawable.ic_megaphone));
        homeEntrances.add(new ModelHomeEntrance("电影", R.drawable.ic_movie));
        homeEntrances.add(new ModelHomeEntrance("讨论", R.drawable.ic_speech));
        homeEntrances.add(new ModelHomeEntrance("磁力", R.drawable.ic_magnet));
        //  homeEntrances.add(new ModelHomeEntrance("我的", R.drawable.ic_hat));
        homeEntrances.add(new ModelHomeEntrance("品茶", R.drawable.ic_cocktails));

        //第2页显示
        //  homeEntrances.add(new ModelHomeEntrance("地图", R.drawable.ic_map));

        homeEntrances.add(new ModelHomeEntrance("视频", R.drawable.ic_tv));
        homeEntrances.add(new ModelHomeEntrance("小说", R.drawable.ic_book));
        homeEntrances.add(new ModelHomeEntrance("文件", R.drawable.ic_folder));
        homeEntrances.add(new ModelHomeEntrance("设置", R.drawable.ic_setting));
        //  homeEntrances.add(new ModelHomeEntrance("升级", R.drawable.ic_rocket));
    }
    private void init() {
        mPageMenuLayout.setPageDatas(homeEntrances, new PageMenuViewHolderCreator() {
            @Override
            public AbstractHolder createHolder(View itemView) {
                return new AbstractHolder<ModelHomeEntrance>(itemView) {
                    private TextView entranceNameTextView;
                    private ImageView entranceIconImageView;

                    @Override
                    protected void initView(View itemView) {
                        entranceIconImageView = itemView.findViewById(R.id.entrance_image);
                        entranceNameTextView = itemView.findViewById(R.id.entrance_name);
                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int) ((float) ScreenUtil.getScreenWidth() / 4.0f));
                        itemView.setLayoutParams(layoutParams);
                    }

                    @Override
                    public void bindView(RecyclerView.ViewHolder holder, final ModelHomeEntrance data, int pos) {
                        entranceNameTextView.setText(data.getName());
                        entranceIconImageView.setImageResource(data.getImage());
                        holder.itemView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //Toast.makeText(getActivity(), data.getName(), Toast.LENGTH_SHORT).show();
                                String clickName =   data.getName();
                                if(clickName.equals("电影")){
                                    //获取edittext的数据
                                    String et = et_search.getText().toString();
                                    Intent intent=new Intent();
                                    intent.putExtra("search", et);//设置参数,""
                                    intent.setClass(homeActivity.this,  movieListActivity.class);//从哪里跳到哪里
                                    homeActivity.this.startActivity(intent);
                                }else if(clickName.equals("聊天")){
//                                    Intent intent = new Intent(homeActivity.this, deviceTypeActivity.class);
//                                    startActivity(intent);
                                }else if(clickName.equals("磁力")){

                                    //获取edittext的数据
                                    String et = et_search.getText().toString();
                                    if(!et.isEmpty()){

                                        Intent intent=new Intent();
                                        intent.putExtra("search", et);//设置参数,""
                                        intent.setClass(homeActivity.this,  magnetActivity.class);//从哪里跳到哪里
                                        homeActivity.this.startActivity(intent);
                                    }else{
                                        Toast.makeText(homeActivity.this, "搜索栏输入为空", Toast.LENGTH_SHORT).show();
                                    }


                                }else if(clickName.equals("我的")){

                                }else if(clickName.equals("文件")){

                                }
                                else if(clickName.equals("设置")){
//                                    Intent intent = new Intent(homeActivity.this, settingActivity.class);
//                                    startActivity(intent);
                                }else if(clickName.equals("品茶")) {

                                    String et = et_search.getText().toString();
                                    Intent intent=new Intent();
                                    intent.putExtra("search", et);//设置参数,""
                                    intent.setClass(homeActivity.this,  youngladyListActivity.class);//从哪里跳到哪里
                                    homeActivity.this.startActivity(intent);


                                }
                                else if(clickName.equals("视频")){
                                    String et = et_search.getText().toString();
                                    Intent intent=new Intent();
                                    intent.putExtra("search", et);//设置参数,""
                                    intent.setClass(homeActivity.this,  tvListActivity.class);//从哪里跳到哪里
                                    homeActivity.this.startActivity(intent);
                                }

                            }
                        });
                    }
                };
            }

            @Override
            public int getLayoutId() {
                return R.layout.item_home_entrance;
            }
        });
        entranceIndicatorView.setIndicatorCount(mPageMenuLayout.getPageCount());
        mPageMenuLayout.setOnPageListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                entranceIndicatorView.setCurrentIndicator(position);
            }
        });
    }


    public void okhttp_text(String search) {


        //创建okHttpclient的对象
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建Request,Builder
        Request.Builder builder = new Request.Builder();
        //使用Request.Builder对象,调用Url方法,传入网路路径
        Request.Builder url = builder.url("http://103.45.145.67/testfolder/ylsearch.php?content="+search);
        //使用Request.Builder对象,调用builder方法构件request对象
        Request request = url.build();
        //创建一个Call对象,参数就request对象
        Call call = okHttpClient.newCall(request);
        //使用call对象,调用enqueue方法,请求加入调度(异步加载)

        call.enqueue(new Callback() {
            //当请求失败的时候,调用此方法
            @Override
            public void onFailure(Call call, IOException e) {
                //andler.sendEmptyMessage(FALL);
            }

            //当请求成功的时候,调用此方法
            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String source = response.body().string();
                System.out.println(source);

                // vod_id,vod_name,vod_class,vod_year,vod_pic
                //开始循环遍历以上字段
//list_c

                List<younglady_C> ls = JSON.parseArray(source, younglady_C.class);
                for (younglady_C pp : ls) {
                    younglady_c = new younglady_C();
                    //mty_id,mty_province,mty_date_add,mty_feature,mty_business,mty_price,mty_photos;
                    String mty_id = pp.getMty_id();
                    String mty_province = pp.getMty_province();
                    String mty_date_add = pp.getMty_date_add();
                    String mty_feature = pp.getMty_feature();
                    String mty_business = pp.getMty_business();
                    String mty_price = pp.getMty_price();
                    String mty_photos = pp.getMty_photos();

                    String photourl[] =  mty_photos.split(",");


                    younglady_c.setMty_id(mty_id);
                    younglady_c.setMty_province(mty_province);
                    younglady_c.setMty_date_add("更新:"+mty_date_add);
                    younglady_c.setMty_feature(mty_feature);
                    younglady_c.setMty_business("时间:"+mty_business);
                    younglady_c.setMty_price("价格一览:"+mty_price);
                    younglady_c.setMty_photos("http://103.45.145.67/testsex/ps/"+photourl[0]);


                    younglady_cs.add(younglady_c);

                }
                Message msg = new Message();
                msg.what = 21;
                Thandler.sendMessage(msg);//用activity中的handler发送消息


            }
        });

    }
    private Handler Thandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 21:
                    clickData((ArrayList<younglady_C>) younglady_cs);
                    break;

            }
        }
    };
    public void clickData(ArrayList<younglady_C> younglady_cs) {

        younglady_a = new younglady_A(R.layout.item_younglady, homeActivity.this, younglady_cs);


        lv_list.setAdapter(younglady_a);
        lv_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // Toast.makeText(ChartActivity.this, "Click item" + i, Toast.LENGTH_SHORT).show();
                final TextView mty_id = (TextView) view.findViewById(R.id.mty_id);//id

                System.out.println();

//                // 带参跳转
                Intent intent=new Intent();
                intent.putExtra("Activity","homeActivity");//传一个页面过去
                intent.putExtra("mty_id", mty_id.getText().toString());//传一个id过去
                intent.setClass(homeActivity.this, youngladyDetailActivity.class);//从哪里跳到哪里
                homeActivity.this.startActivity(intent);


            }
        });


    }

}
